package com.oa.gform.client.ui;


import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NodeList;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.oa.gform.client.ui.datamodel.GDataSourceParam;
import com.oa.gform.client.ui.datasource.GDataSource;
import com.oa.gform.client.ui.event.GRepeaterItemClickEvent;
import com.oa.gform.client.ui.event.GRepeaterItemClickHandler;
import com.oa.gform.client.ui.event.HasGRepeaterItemClickHandler;
/***
 * Implementazione di un fieldset che contine una tabella 
 * Utilizzabile anche con ui:binder
 * <p>
 * <img class='gallery' src='doc-files/VerticalPanel.png'/>
 * </p>
 */

public class GRepeater<ElementType> extends ScrollPanel implements HasGRepeaterItemClickHandler<ElementType>,HasGDatasource<ElementType>{
	VerticalPanel intPanel = new VerticalPanel();
	Widget repeaterItem = new Widget();
	
	GBaseWidgetDatasourceImpl<ElementType> baseImpl = new GBaseWidgetDatasourceImpl<ElementType>();
	
	/**
	 * Crea un nuovo field set basato su due colonne
	 */
	
	public GRepeater() {
		super.add(intPanel);
	}

	
	@Override
	public void add(Widget w) {
		
		repeaterItem = w;
	}
	
	/***
	 * Esegue il render del repeater invocando prima il load 
	 * dei dati del datasource.
	 * 
	 */
	public void render() {
		render(null,true);
	}
	
	
	
	
	/***
	 * Esegue il render del repeater.
	 * 
	 */
	public void render(GDataSourceParam param,boolean loadData) {
		if (loadData) {
			baseImpl.loadData(param,new Command() {
				public void execute() {
					renderImplementation();		
				}
			});		
		} else {
			renderImplementation();			
		}
	}
	
	private void renderImplementation() {
		for (ElementType item : baseImpl.getData()) {
			
		} 
		
		for (int i = 0; i < 10; i++) {
			Widget w = repeaterItem;
			String html = repeaterItem.toString().replaceAll("\\#\\[NomeComune\\]", "comune" + i);
			HTMLPanel item = new HTMLPanel(html);
			final int itemIndex = i;
			NodeList<Element> linkNodes = item.getElement().getElementsByTagName("a");
			for (int j = 0; j < linkNodes.getLength(); j++) {
				String innerHTML = linkNodes.getItem(j).getInnerHTML();
				Element eLink = linkNodes.getItem(j);
				final String actionName = eLink.getAttribute("action");
				HTML link = new HTML(innerHTML);
				link.addClickHandler(new ClickHandler() {
					public void onClick(ClickEvent event) {
						// Invoco gli handler registrati
						
						Log.trace("Invoke event GRepeaterItemClickEvent: Action Name=" + actionName );
						//GRepeaterItemClickEvent event = new GRepeaterItemClickEvent();
						
						//TODO FIRE EVENT 
						//GRepeaterItemClickEvent.fire(GRepeater.this,GRepeater.this,actionName,itemIndex);
					}
				});
				// Sostituisco l'element a con il nuovo elemento con l'evento click
				item.addAndReplaceElement(link, (com.google.gwt.user.client.Element) eLink);
			}
			intPanel.add(item);
			w.toString();
		}
	}


	@Override
	/***
	 * Agginge un handler per la gestione dei click sui repater item
	 * che vengono alzati tramite i tag <A> sul codice HTML inserito nella 
	 * GRepeaterItem
	 */
	public HandlerRegistration addGRepeaterItemClickHandler(GRepeaterItemClickHandler<?> handler) {
		return addHandler(handler, GRepeaterItemClickEvent.getType());
		
	}
	
	
	public void addLoadDataListener(Command onLoadData) {
		baseImpl.addLoadDataListener(onLoadData);
	}


	public GDataSource<ElementType> getGDatasource() {
		return baseImpl.getGDatasource();
	}


	public void invokeLoadDataEnd() {
		baseImpl.invokeLoadDataEnd();
	}


	public boolean isCacheEnabled() {
		return baseImpl.isCacheEnabled();
	}

	@Override
	public void loadData() {
		baseImpl.loadData();
	}
	
	@Override
	public void loadData(GDataSourceParam param) {
		baseImpl.loadData(param);
	}
	
	@Override
	public void loadData(GDataSourceParam param, Command callback) {
		baseImpl.loadData(param, callback);
	}

	public void setCacheEnabled(boolean cacheEnabled) {
		baseImpl.setCacheEnabled(cacheEnabled);
	}


	public void setGDatasource(GDataSource<ElementType> datasource) {
		baseImpl.setGDatasource(datasource);
	}




}
